package com.eascs.admin.o2o.loan.utils;

import java.util.ArrayList;
import java.util.List;

import com.eascs.common.util.StringUtil;

public class BankNoCheckUtil {
	/**
	 * 银行卡号不能为空
	 */
	public static String ISNOTNULL = "ISNOTNULL";// 银行卡号不能为空
	/**
	 * 银行卡号须16到19之间
	 */
	public static String IS1619 = "IS1619";// 银行卡号须16到19之间
	/**
	 * 银行卡号必须全为数字
	 */
	public static String ISNUMBER = "ISNUMBER";// 银行卡号必须全为数字
	/**
	 * 银行卡号不符合规范
	 */
	public static String ISNOT = "ISNOT";// 银行卡号不符合规范
	/**
	 * 银行卡号符合规范
	 */
	public static String YEAS = "YEAS";// 银行卡号符合规范

	public static String checkBankNo(String bankno) {
		String num = "^\\d*$";// 校验全是数字
		String strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";// 开头6位
		if (StringUtil.isEmpty(bankno)) {
			return ISNOTNULL;
		}
		if (bankno.length() < 16 || bankno.length() > 19) {
			return IS1619;
		}
		if (!bankno.matches(num)) {
			return ISNUMBER;// "银行卡号必须全为数字";
		}
		if (strBin.indexOf(bankno.substring(0, 2)) == -1) {
			return ISNOT;
		}

		int lastNum = Integer.parseInt(bankno.substring(bankno.length() - 1, bankno.length()));// 取出最后一位（与luhm进行比较）

		String first15Num = bankno.substring(0, bankno.length() - 1);// 前15或18位
		char[] ch = first15Num.toCharArray();
		List<String> newArr = new ArrayList<String>();
		for (int i = ch.length - 1; i > -1; i--) { // 前15或18位倒序存进数组
			newArr.add(ch[i] + "");
		}
		List<Integer> arrJiShu = new ArrayList<Integer>(); // 奇数位*2的积 <9
		List<Integer> arrJiShu2 = new ArrayList<Integer>(); // 奇数位*2的积 >9

		List<Integer> arrOuShu = new ArrayList<Integer>(); // 偶数位数组
		for (int j = 0; j < newArr.size(); j++) {
			if ((j + 1) % 2 == 1) {// 奇数位
				if (Integer.parseInt(newArr.get(j)) * 2 < 9)
					arrJiShu.add(Integer.parseInt(newArr.get(j)) * 2);
				else
					arrJiShu2.add(Integer.parseInt(newArr.get(j)) * 2);
			} else // 偶数位
				arrOuShu.add(Integer.parseInt(newArr.get(j)));
		}

		List<Integer> jishu_child1 = new ArrayList<Integer>();// 奇数位*2 >9
																// 的分割之后的数组个位数
		List<Integer> jishu_child2 = new ArrayList<Integer>();// 奇数位*2 >9
																// 的分割之后的数组十位数
		for (int h = 0; h < arrJiShu2.size(); h++) {
			jishu_child1.add(arrJiShu2.get(h) % 10);
			jishu_child2.add(arrJiShu2.get(h) / 10);
		}

		int sumJiShu = 0; // 奇数位*2 < 9 的数组之和
		int sumOuShu = 0; // 偶数位数组之和
		int sumJiShuChild1 = 0; // 奇数位*2 >9 的分割之后的数组个位数之和
		int sumJiShuChild2 = 0; // 奇数位*2 >9 的分割之后的数组十位数之和
		int sumTotal = 0;
		for (int m = 0; m < arrJiShu.size(); m++) {
			sumJiShu = sumJiShu + arrJiShu.get(m);
		}

		for (int n = 0; n < arrOuShu.size(); n++) {
			sumOuShu = sumOuShu + arrOuShu.get(n);
		}

		for (int p = 0; p < jishu_child1.size(); p++) {
			sumJiShuChild1 = sumJiShuChild1 + jishu_child1.get(p);
			sumJiShuChild2 = sumJiShuChild2 + jishu_child2.get(p);
		}
		// 计算总和
		sumTotal = sumJiShu + sumOuShu + sumJiShuChild1 + sumJiShuChild2;

		// 计算Luhm值
		int k = sumTotal % 10 == 0 ? 10 : sumTotal % 10;
		int luhm = 10 - k;

		if (lastNum == luhm) {
			return YEAS;// "银行卡号符合规范");
		} else {
			return ISNOT;// "银行卡号不符合规范");
		}
	}
	
	public static void main(String[] args) {
		//181020100100227784
		System.out.println(BankNoCheckUtil.checkBankNo("6212264000019213236"));
	}
}
